package com.dmxh.consumer;

import cn.hutool.core.util.RandomUtil;
import com.dmxh.api.SmsAPI;
import com.dmxh.pojo.Deadqueue;
import com.dmxh.result.Result;
import com.dmxh.result.ResultConstant;
import com.dmxh.service.DeadqueueService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@RocketMQMessageListener(consumerGroup = "dongdongshop-consumer",topic = "dongdongshop-t")
public class ConsumerMessage implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {

    @Autowired
    private DeadqueueService ds;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SmsAPI smsAPI;

    @Override
    public void onMessage(String message) {
        System.out.println("消费的消息是"+message);
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {

                MessageExt messageExt = list.get(0);
                String phone = new String(messageExt.getBody());
                try {
                    System.out.println("接收手机号:"+phone);
                    String code = null;
                    //Redis中是否发送过验证码
                    Object o = redisTemplate.boundValueOps("register:" + phone).get();
                    if (o == null) {
                        //第一次发送
                        //生成随机验证码
                        code = RandomUtil.randomNumbers(6);
                        redisTemplate.boundValueOps("register:"+phone).set(code+":"+System.currentTimeMillis(),5, TimeUnit.MINUTES);
                    } else {
                        //已经发送过验证码
                        //验证发送时间是否超过一分钟
                        String time= redisTemplate.boundValueOps("register:" + phone).get().toString().split(":")[1];
                        if (System.currentTimeMillis()-Long.valueOf(time) < 1 * 60 * 1000) {
                            System.out.println(Result.response(ResultConstant.TIME_LIMIT_ONE_MINUTE));
                        }
                        //发送时间已超过一分钟
                        code = redisTemplate.boundValueOps("register:" + phone).get().toString().split(":")[0];
                    }
                    smsAPI.userSms(code,phone);
                    System.out.println("redis-->"+code);
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                } catch (Exception e) {
                    if (messageExt.getReconsumeTimes() >= 1) {
                        Deadqueue deadqueue = new Deadqueue();
                        deadqueue.setBody(phone);
                        deadqueue.setError(e.getMessage());
                        deadqueue.setTopic("dongdongshop-g");
                        deadqueue.setMessageid(messageExt.getMsgId());
                        deadqueue.setTime(new Date());
                        deadqueue.setStatus(0);
                        ds.add(deadqueue);
                        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
            }
        });
    }
}
